package com.sansecy.mocklocationtest.apklist;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sansecy.mocklocationtest.AppInfo;
import com.sansecy.mocklocationtest.R;
import com.sansecy.mocklocationtest.getInstalledApps;

import java.util.ArrayList;

public class ApplistFragement extends Fragment {
    private static final String TAG = "ApplistFragement-App";
    public static ApplistFragement newInstance() {

        Bundle args = new Bundle();

        ApplistFragement fragment = new ApplistFragement();
        fragment.setArguments(args);
        return fragment;
    }
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_app_list, container, false);

        RecyclerView recyclerView = view.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new GridLayoutManager(requireActivity(), 4));
        AppListAdapter adapter = new AppListAdapter(requireActivity());
        recyclerView.setAdapter(adapter);
//        PackageManager pm = getPackageManager();
        ////获取到所有安装了的应用程序的信息，包括那些卸载了的，但没有清除数据的应用程序
//        List<PackageInfo> installedPackages = pm.getInstalledPackages(PackageManager.GET_ACTIVITIES);

        getInstalledApps getInstalledApps = new getInstalledApps();
        getInstalledApps.setArrayListOnCompleteListener(new getInstalledApps.OnCompleteListener<ArrayList<AppInfo>>() {
            @Override
            public void onComplete(ArrayList<AppInfo> appInfos) {
                Log.d(TAG, "onComplete() called with: appInfos = [" + appInfos + "]");
                adapter.setData(appInfos);
            }
        });
        getInstalledApps.execute();
        return view;
    }
}
